//
//  RBGroupDetailGroupItemView.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/27.
//

#import "RBGroupDetailGroupItemView.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBPager.h"
#import "RanTool.h"
#import "UIImageView+RBAdd.h"
#import "RBTimer.h"

@interface RBGroupDetailGroupItemView()

@property(nonatomic, strong)IBOutlet UIImageView *imgView;
@property(nonatomic, strong)IBOutlet UILabel *titleL;
@property(nonatomic, strong)IBOutlet UILabel *timeL;
@property(nonatomic, strong)IBOutlet UILabel *stateL;

@property(nonatomic, strong) RBTimer *timer;

@end

@implementation RBGroupDetailGroupItemView

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self zoomSubViewsWithSelf];
    [self.stateL rbAddTapGesWithTarget:self action:@selector(joinBtnTapped)];
    [self.stateL extendResponseArea];
    __weak typeof(self) weak_self = self;
    [self.timer startTimerWithInterval:1 block:^(float countF, NSInteger countI) {
        [weak_self reloadTimeDisplay];
    }];
}

- (void)setModel:(RBGroupDetailAtyModel *)model
{
    _model = model;
    [self.imgView setImageWithUrl:model.member_avatar placeHolder:nil];
    self.titleL.text = model.member_mobile;
    [self reloadTimeDisplay];
}

- (void)reloadTimeDisplay
{
    if(!self.model) return;
    self.timeL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        UIColor *blackColor = [RBThemeConfig textBlackColor];
        UIColor *redColor = [UIColor colorWithHexString:@"#F80707"];
        UIFont *font = [RanTool numFontWithSize:Zoom(12)];
        CGFloat spacing = Zoom(1);
        [maker addAttributeStringWithFont:font color:blackColor string:@"还差" lineSpacing:spacing];
        [maker addAttributeStringWithFont:font color:redColor string:[NSString stringWithFormat:@"%zd人", self.model.surplus_num] lineSpacing:spacing];
        [maker addAttributeStringWithFont:font color:blackColor string:@"成团" lineSpacing:spacing];
        [maker addAttributeStringWithFont:font color:blackColor string:[NSString stringWithFormat:@"\n剩余%@", [self countDownTime]] lineSpacing:spacing];
    }];
}

- (NSString *)countDownTime
{
    CGFloat timeStamp = self.model.end_time - [NSDate.date timeIntervalSince1970];
    timeStamp = MAX(timeStamp, 0);
    NSArray *timeArray = [RanTool dayHourMinuteSecondFromInterval:timeStamp];
    NSMutableString *stringM = NSMutableString.string;
    NSString *dayStr = timeArray[0];
    if(dayStr.intValue)
    {
        [stringM appendFormat:@"%@天",timeArray[0]];
    }
    [stringM appendFormat:@"%@：%@：%@",timeArray[1],timeArray[2],timeArray[3]];
    return stringM;
}

#pragma mark - Event

- (void)joinBtnTapped
{
    if(self.joinBlock)
    {
        self.joinBlock(self.model);
    }
}

- (RBTimer *)timer
{
    if(!_timer)
    {
        self.timer = RBTimer.new;
    }
    return _timer;
}

@end
